package com.example.proxybean.interceptor.impl;

import com.example.proxybean.interceptor.MyInterceptor;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class MyInterceptorImpl implements MyInterceptor {
    @Override
    public void before() {
        System.out.println("before ...");
    }

    @Override
    public void after() {
        System.out.println("after ...");
    }

    @Override
    public boolean useAround() {
        return true;
    }

    @Override
    public void around(Object target, Method method, Object[] params) {
        if (useAround()){
            before();
            try {
                method.invoke(target,params);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            after();
        }else {
            try {
                method.invoke(target,params);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

}
